/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.DAO;

import com.Entity.EmployeeLanguages;
import com.Interface.EmployeeLanguageInterface;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Vi
 */
@Stateless
public class EmployeeLanguageDAO implements EmployeeLanguageInterface{
    
      private EntityManager entityManager;
      
      public EmployeeLanguageDAO() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public List<EmployeeLanguages> getEmployeeLanguages(int idEmployee) {
        return (List<EmployeeLanguages>)entityManager.createQuery( "select el from EmployeeLanguages el where el.idEmployee = " + idEmployee ).getResultList();
    }
    
    @Override
    public EmployeeLanguages getEmployeeLanguage(int idEmployee, int idLanguage) {
        return (EmployeeLanguages)entityManager.createQuery( "select el from EmployeeLanguages el where el.idEmployee = " + idEmployee + " and el.idLanguage = " + idLanguage ).getSingleResult();
    }

    @Override
    public void addEmployeeLanguages(List<EmployeeLanguages> employeeLanguages) {
        entityManager.getTransaction().begin();
        for(EmployeeLanguages el : employeeLanguages)
        {
            entityManager.persist(el);
        }
        entityManager.getTransaction().commit();
    }
    
    @Override
    public void removeEmployeeLanguages(Integer idEmployee) {
        List<EmployeeLanguages> employeeLanguages = getEmployeeLanguages(idEmployee);
        entityManager.getTransaction().begin();
        for(EmployeeLanguages el : employeeLanguages)
        {
            entityManager.remove(el);
        }
        entityManager.getTransaction().commit();
    }

    @Override
    public void updateEmploteeLanguages(Integer idEmployee, List<EmployeeLanguages> employeeLanguages) {
        
        removeEmployeeLanguages(idEmployee);
        addEmployeeLanguages(employeeLanguages);
        
    }
}
